Skip to content

Commit 0039f52

Browse files
Shubhamgreenrobot-team
authored andcommitted
.gitignore: check in generated files to the repo
This is how a user project should do it, also makes it easier to just clone and run the example.
1 parent 99db487 commit 0039f52

File tree

3 files changed

+738
-1
lines changed

3 files changed

+738
-1
lines changed

Example/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
// Generated using the ObjectBox Swift Generator — https://objectbox.io
2+
// DO NOT EDIT
3+
4+
// swiftlint:disable all
5+
import ObjectBox
6+
import Foundation
7+
8+
// MARK: - Entity metadata
9+
10+
extension Author: ObjectBox.Entity {}
11+
12+
extension Author: ObjectBox.__EntityRelatable {
13+
internal typealias EntityType = Author
14+
15+
internal var _id: EntityId<Author> {
16+
return EntityId<Author>(self.id.value)
17+
}
18+
}
19+
20+
extension Author: ObjectBox.EntityInspectable {
21+
internal typealias EntityBindingType = AuthorBinding
22+
23+
/// Generated metadata used by ObjectBox to persist the entity.
24+
internal static let entityInfo = ObjectBox.EntityInfo(name: "Author", id: 1)
25+
26+
internal static let entityBinding = EntityBindingType()
27+
28+
fileprivate static func buildEntity(modelBuilder: ObjectBox.ModelBuilder) throws {
29+
let entityBuilder = try modelBuilder.entityBuilder(for: Author.self, id: 1, uid: 712683617673955584)
30+
try entityBuilder.addProperty(name: "id", type: PropertyType.long, flags: [.id], id: 1, uid: 6336800942024279296)
31+
try entityBuilder.addProperty(name: "name", type: PropertyType.string, id: 2, uid: 5025387500910526208)
32+
33+
try entityBuilder.lastProperty(id: 2, uid: 5025387500910526208)
34+
}
35+
}
36+
37+
extension Author {
38+
/// Generated entity property information.
39+
///
40+
/// You may want to use this in queries to specify fetch conditions, for example:
41+
///
42+
/// box.query { Author.id == myId }
43+
internal static var id: Property<Author, Id, Id> { return Property<Author, Id, Id>(propertyId: 1, isPrimaryKey: true) }
44+
/// Generated entity property information.
45+
///
46+
/// You may want to use this in queries to specify fetch conditions, for example:
47+
///
48+
/// box.query { Author.name.startsWith("X") }
49+
internal static var name: Property<Author, String, Void> { return Property<Author, String, Void>(propertyId: 2, isPrimaryKey: false) }
50+
/// Use `Author.notes` to refer to this ToMany relation property in queries,
51+
/// like when using `QueryBuilder.and(property:, conditions:)`.
52+
53+
internal static var notes: ToManyProperty<Note> { return ToManyProperty(.valuePropertyId(6)) }
54+
55+
56+
fileprivate func __setId(identifier: ObjectBox.Id) {
57+
self.id = Id(identifier)
58+
}
59+
}
60+
61+
extension ObjectBox.Property where E == Author {
62+
/// Generated entity property information.
63+
///
64+
/// You may want to use this in queries to specify fetch conditions, for example:
65+
///
66+
/// box.query { .id == myId }
67+
68+
internal static var id: Property<Author, Id, Id> { return Property<Author, Id, Id>(propertyId: 1, isPrimaryKey: true) }
69+
70+
/// Generated entity property information.
71+
///
72+
/// You may want to use this in queries to specify fetch conditions, for example:
73+
///
74+
/// box.query { .name.startsWith("X") }
75+
76+
internal static var name: Property<Author, String, Void> { return Property<Author, String, Void>(propertyId: 2, isPrimaryKey: false) }
77+
78+
/// Use `.notes` to refer to this ToMany relation property in queries, like when using
79+
/// `QueryBuilder.and(property:, conditions:)`.
80+
81+
internal static var notes: ToManyProperty<Note> { return ToManyProperty(.valuePropertyId(6)) }
82+
83+
}
84+
85+
86+
/// Generated service type to handle persisting and reading entity data. Exposed through `Author.EntityBindingType`.
87+
internal final class AuthorBinding: ObjectBox.EntityBinding, Sendable {
88+
internal typealias EntityType = Author
89+
internal typealias IdType = Id
90+
91+
internal required init() {}
92+
93+
internal func generatorBindingVersion() -> Int { 1 }
94+
95+
internal func setEntityIdUnlessStruct(of entity: EntityType, to entityId: ObjectBox.Id) {
96+
entity.__setId(identifier: entityId)
97+
}
98+
99+
internal func entityId(of entity: EntityType) -> ObjectBox.Id {
100+
return entity.id.value
101+
}
102+
103+
internal func collect(fromEntity entity: EntityType, id: ObjectBox.Id,
104+
propertyCollector: ObjectBox.FlatBufferBuilder, store: ObjectBox.Store) throws {
105+
let propertyOffset_name = propertyCollector.prepare(string: entity.name)
106+
107+
propertyCollector.collect(id, at: 2 + 2 * 1)
108+
propertyCollector.collect(dataOffset: propertyOffset_name, at: 2 + 2 * 2)
109+
}
110+
111+
internal func postPut(fromEntity entity: EntityType, id: ObjectBox.Id, store: ObjectBox.Store) throws {
112+
if entityId(of: entity) == 0 { // New object was put? Attach relations now that we have an ID.
113+
let notes = ToMany<Note>.backlink(
114+
sourceBox: store.box(for: ToMany<Note>.ReferencedType.self),
115+
sourceProperty: ToMany<Note>.ReferencedType.author,
116+
targetId: EntityId<Author>(id.value))
117+
if !entity.notes.isEmpty {
118+
notes.replace(entity.notes)
119+
}
120+
entity.notes = notes
121+
try entity.notes.applyToDb()
122+
}
123+
}
124+
internal func createEntity(entityReader: ObjectBox.FlatBufferReader, store: ObjectBox.Store) -> EntityType {
125+
let entity = Author()
126+
127+
entity.id = entityReader.read(at: 2 + 2 * 1)
128+
entity.name = entityReader.read(at: 2 + 2 * 2)
129+
130+
entity.notes = ToMany<Note>.backlink(
131+
sourceBox: store.box(for: ToMany<Note>.ReferencedType.self),
132+
sourceProperty: ToMany<Note>.ReferencedType.author,
133+
targetId: EntityId<Author>(entity.id.value))
134+
return entity
135+
}
136+
}
137+
138+
139+
140+
extension Note: ObjectBox.__EntityRelatable {
141+
internal typealias EntityType = Note
142+
143+
internal var _id: EntityId<Note> {
144+
return EntityId<Note>(self.id.value)
145+
}
146+
}
147+
148+
extension Note: ObjectBox.EntityInspectable {
149+
internal typealias EntityBindingType = NoteBinding
150+
151+
/// Generated metadata used by ObjectBox to persist the entity.
152+
internal static let entityInfo = ObjectBox.EntityInfo(name: "Note", id: 2)
153+
154+
internal static let entityBinding = EntityBindingType()
155+
156+
fileprivate static func buildEntity(modelBuilder: ObjectBox.ModelBuilder) throws {
157+
let entityBuilder = try modelBuilder.entityBuilder(for: Note.self, id: 2, uid: 5608901830082711040)
158+
try entityBuilder.addProperty(name: "id", type: PropertyType.long, flags: [.id], id: 1, uid: 7180411752564202752)
159+
try entityBuilder.addProperty(name: "title", type: PropertyType.string, id: 2, uid: 249105953415333376)
160+
try entityBuilder.addProperty(name: "text", type: PropertyType.string, id: 3, uid: 5661281725891017216)
161+
try entityBuilder.addProperty(name: "creationDate", type: PropertyType.date, id: 4, uid: 8342334437465755392)
162+
try entityBuilder.addProperty(name: "modificationDate", type: PropertyType.date, id: 5, uid: 8881960381068888832)
163+
try entityBuilder.addToOneRelation(name: "author", targetEntityInfo: ToOne<Author>.Target.entityInfo, flags: [.indexed, .indexPartialSkipZero], id: 6, uid: 6001769173142034944, indexId: 1, indexUid: 6069708401898380544)
164+
165+
try entityBuilder.lastProperty(id: 6, uid: 6001769173142034944)
166+
}
167+
}
168+
169+
extension Note {
170+
/// Generated entity property information.
171+
///
172+
/// You may want to use this in queries to specify fetch conditions, for example:
173+
///
174+
/// box.query { Note.id == myId }
175+
internal static var id: Property<Note, Id, Id> { return Property<Note, Id, Id>(propertyId: 1, isPrimaryKey: true) }
176+
/// Generated entity property information.
177+
///
178+
/// You may want to use this in queries to specify fetch conditions, for example:
179+
///
180+
/// box.query { Note.title.startsWith("X") }
181+
internal static var title: Property<Note, String, Void> { return Property<Note, String, Void>(propertyId: 2, isPrimaryKey: false) }
182+
/// Generated entity property information.
183+
///
184+
/// You may want to use this in queries to specify fetch conditions, for example:
185+
///
186+
/// box.query { Note.text.startsWith("X") }
187+
internal static var text: Property<Note, String, Void> { return Property<Note, String, Void>(propertyId: 3, isPrimaryKey: false) }
188+
/// Generated entity property information.
189+
///
190+
/// You may want to use this in queries to specify fetch conditions, for example:
191+
///
192+
/// box.query { Note.creationDate > 1234 }
193+
internal static var creationDate: Property<Note, Date?, Void> { return Property<Note, Date?, Void>(propertyId: 4, isPrimaryKey: false) }
194+
/// Generated entity property information.
195+
///
196+
/// You may want to use this in queries to specify fetch conditions, for example:
197+
///
198+
/// box.query { Note.modificationDate > 1234 }
199+
internal static var modificationDate: Property<Note, Date?, Void> { return Property<Note, Date?, Void>(propertyId: 5, isPrimaryKey: false) }
200+
internal static var author: Property<Note, EntityId<ToOne<Author>.Target>, ToOne<Author>.Target> { return Property(propertyId: 6) }
201+
202+
203+
fileprivate func __setId(identifier: ObjectBox.Id) {
204+
self.id = Id(identifier)
205+
}
206+
}
207+
208+
extension ObjectBox.Property where E == Note {
209+
/// Generated entity property information.
210+
///
211+
/// You may want to use this in queries to specify fetch conditions, for example:
212+
///
213+
/// box.query { .id == myId }
214+
215+
internal static var id: Property<Note, Id, Id> { return Property<Note, Id, Id>(propertyId: 1, isPrimaryKey: true) }
216+
217+
/// Generated entity property information.
218+
///
219+
/// You may want to use this in queries to specify fetch conditions, for example:
220+
///
221+
/// box.query { .title.startsWith("X") }
222+
223+
internal static var title: Property<Note, String, Void> { return Property<Note, String, Void>(propertyId: 2, isPrimaryKey: false) }
224+
225+
/// Generated entity property information.
226+
///
227+
/// You may want to use this in queries to specify fetch conditions, for example:
228+
///
229+
/// box.query { .text.startsWith("X") }
230+
231+
internal static var text: Property<Note, String, Void> { return Property<Note, String, Void>(propertyId: 3, isPrimaryKey: false) }
232+
233+
/// Generated entity property information.
234+
///
235+
/// You may want to use this in queries to specify fetch conditions, for example:
236+
///
237+
/// box.query { .creationDate > 1234 }
238+
239+
internal static var creationDate: Property<Note, Date?, Void> { return Property<Note, Date?, Void>(propertyId: 4, isPrimaryKey: false) }
240+
241+
/// Generated entity property information.
242+
///
243+
/// You may want to use this in queries to specify fetch conditions, for example:
244+
///
245+
/// box.query { .modificationDate > 1234 }
246+
247+
internal static var modificationDate: Property<Note, Date?, Void> { return Property<Note, Date?, Void>(propertyId: 5, isPrimaryKey: false) }
248+
249+
internal static var author: Property<Note, ToOne<Author>.Target.EntityBindingType.IdType, ToOne<Author>.Target> { return Property<Note, ToOne<Author>.Target.EntityBindingType.IdType, ToOne<Author>.Target>(propertyId: 6) }
250+
251+
}
252+
253+
254+
/// Generated service type to handle persisting and reading entity data. Exposed through `Note.EntityBindingType`.
255+
internal final class NoteBinding: ObjectBox.EntityBinding, Sendable {
256+
internal typealias EntityType = Note
257+
internal typealias IdType = Id
258+
259+
internal required init() {}
260+
261+
internal func generatorBindingVersion() -> Int { 1 }
262+
263+
internal func setEntityIdUnlessStruct(of entity: EntityType, to entityId: ObjectBox.Id) {
264+
entity.__setId(identifier: entityId)
265+
}
266+
267+
internal func entityId(of entity: EntityType) -> ObjectBox.Id {
268+
return entity.id.value
269+
}
270+
271+
internal func collect(fromEntity entity: EntityType, id: ObjectBox.Id,
272+
propertyCollector: ObjectBox.FlatBufferBuilder, store: ObjectBox.Store) throws {
273+
let propertyOffset_title = propertyCollector.prepare(string: entity.title)
274+
let propertyOffset_text = propertyCollector.prepare(string: entity.text)
275+
276+
propertyCollector.collect(id, at: 2 + 2 * 1)
277+
propertyCollector.collect(entity.creationDate, at: 2 + 2 * 4)
278+
propertyCollector.collect(entity.modificationDate, at: 2 + 2 * 5)
279+
try propertyCollector.collect(entity.author, at: 2 + 2 * 6, store: store)
280+
propertyCollector.collect(dataOffset: propertyOffset_title, at: 2 + 2 * 2)
281+
propertyCollector.collect(dataOffset: propertyOffset_text, at: 2 + 2 * 3)
282+
}
283+
284+
internal func postPut(fromEntity entity: EntityType, id: ObjectBox.Id, store: ObjectBox.Store) throws {
285+
if entityId(of: entity) == 0 { // New object was put? Attach relations now that we have an ID.
286+
entity.author.attach(to: store.box(for: Author.self))
287+
}
288+
}
289+
internal func setToOneRelation(_ propertyId: obx_schema_id, of entity: EntityType, to entityId: ObjectBox.Id?) {
290+
switch propertyId {
291+
case 6:
292+
entity.author.targetId = (entityId != nil) ? EntityId<Author>(entityId!) : nil
293+
default:
294+
fatalError("Attempt to change nonexistent ToOne relation with ID \(propertyId)")
295+
}
296+
}
297+
internal func createEntity(entityReader: ObjectBox.FlatBufferReader, store: ObjectBox.Store) -> EntityType {
298+
let entity = Note()
299+
300+
entity.id = entityReader.read(at: 2 + 2 * 1)
301+
entity.title = entityReader.read(at: 2 + 2 * 2)
302+
entity.text = entityReader.read(at: 2 + 2 * 3)
303+
entity.creationDate = entityReader.read(at: 2 + 2 * 4)
304+
entity.modificationDate = entityReader.read(at: 2 + 2 * 5)
305+
306+
entity.author = entityReader.read(at: 2 + 2 * 6, store: store)
307+
return entity
308+
}
309+
}
310+
311+
312+
/// Helper function that allows calling Enum(rawValue: value) with a nil value, which will return nil.
313+
fileprivate func optConstruct<T: RawRepresentable>(_ type: T.Type, rawValue: T.RawValue?) -> T? {
314+
guard let rawValue = rawValue else { return nil }
315+
return T(rawValue: rawValue)
316+
}
317+
318+
// MARK: - Store setup
319+
320+
fileprivate func cModel() throws -> OpaquePointer {
321+
let modelBuilder = try ObjectBox.ModelBuilder()
322+
try Author.buildEntity(modelBuilder: modelBuilder)
323+
try Note.buildEntity(modelBuilder: modelBuilder)
324+
modelBuilder.lastEntity(id: 2, uid: 5608901830082711040)
325+
modelBuilder.lastIndex(id: 1, uid: 6069708401898380544)
326+
return modelBuilder.finish()
327+
}
328+
329+
extension ObjectBox.Store {
330+
/// A store with a fully configured model. Created by the code generator with your model's metadata in place.
331+
///
332+
/// # In-memory database
333+
/// To use a file-less in-memory database, instead of a directory path pass `memory:`
334+
/// together with an identifier string:
335+
/// ```swift
336+
/// let inMemoryStore = try Store(directoryPath: "memory:test-db")
337+
/// ```
338+
///
339+
/// - Parameters:
340+
/// - directoryPath: The directory path in which ObjectBox places its database files for this store,
341+
/// or to use an in-memory database `memory:<identifier>`.
342+
/// - maxDbSizeInKByte: Limit of on-disk space for the database files. Default is `1024 * 1024` (1 GiB).
343+
/// - fileMode: UNIX-style bit mask used for the database files; default is `0o644`.
344+
/// Note: directories become searchable if the "read" or "write" permission is set (e.g. 0640 becomes 0750).
345+
/// - maxReaders: The maximum number of readers.
346+
/// "Readers" are a finite resource for which we need to define a maximum number upfront.
347+
/// The default value is enough for most apps and usually you can ignore it completely.
348+
/// However, if you get the maxReadersExceeded error, you should verify your
349+
/// threading. For each thread, ObjectBox uses multiple readers. Their number (per thread) depends
350+
/// on number of types, relations, and usage patterns. Thus, if you are working with many threads
351+
/// (e.g. in a server-like scenario), it can make sense to increase the maximum number of readers.
352+
/// Note: The internal default is currently around 120. So when hitting this limit, try values around 200-500.
353+
/// - readOnly: Opens the database in read-only mode, i.e. not allowing write transactions.
354+
///
355+
/// - important: This initializer is created by the code generator. If you only see the internal `init(model:...)`
356+
/// initializer, trigger code generation by building your project.
357+
internal convenience init(directoryPath: String, maxDbSizeInKByte: UInt64 = 1024 * 1024,
358+
fileMode: UInt32 = 0o644, maxReaders: UInt32 = 0, readOnly: Bool = false) throws {
359+
try self.init(
360+
model: try cModel(),
361+
directory: directoryPath,
362+
maxDbSizeInKByte: maxDbSizeInKByte,
363+
fileMode: fileMode,
364+
maxReaders: maxReaders,
365+
readOnly: readOnly)
366+
}
367+
}
368+
369+
// swiftlint:enable all

0 commit comments

Comments
 (0)