@sprucelabs/data-stores 25.1.0 → 25.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
15
15
  protected abstract databaseSchema: DatabaseSchema;
16
16
  protected scrambleFields?: string[];
17
17
  protected db: Database;
18
+ protected primaryFieldNames: string[];
18
19
  initialize?(): Promise<void>;
19
20
  protected prepareRecord?<IncludePrivateFields extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(record: DatabaseRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<PrepareResults<FullSchema, IncludePrivateFields>>;
20
21
  protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord, 'id'>>;
@@ -30,6 +31,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
30
31
  protected prepareAndNormalizeRecord<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(record: any, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
31
32
  create<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(values: CreateRecord[], options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>[]>;
32
33
  createOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(values: CreateRecord, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>;
34
+ private get primaryFieldName();
33
35
  findOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> | null>;
34
36
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
35
37
  find<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, queryOptions?: Omit<QueryOptions, 'includeFields'>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Awaited<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>[]>;
@@ -28,6 +28,7 @@ import errorUtil from '../utilities/error.utility.js';
28
28
  export default class AbstractStore extends AbstractMutexer {
29
29
  constructor(db, collectionName) {
30
30
  super();
31
+ this.primaryFieldNames = ['id'];
31
32
  this.db = db;
32
33
  if (collectionName) {
33
34
  this.setCollectionName(collectionName);
@@ -59,9 +60,9 @@ export default class AbstractStore extends AbstractMutexer {
59
60
  const cleanedValues = yield Promise.all(values.map((v) => __awaiter(this, void 0, void 0, function* () { return (this.willCreate ? this.willCreate(v) : v); })));
60
61
  const databaseRecords = cleanedValues.map((v) => {
61
62
  var _a;
62
- return (Object.assign(Object.assign({}, v), {
63
+ return (Object.assign(Object.assign({}, v), { [this.primaryFieldName]:
63
64
  //@ts-ignore
64
- id: (_a = v.id) !== null && _a !== void 0 ? _a : this.db.generateId() }));
65
+ (_a = v[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() }));
65
66
  });
66
67
  const toSave = databaseRecords.map((r) => normalizeSchemaValues(this.databaseSchema,
67
68
  //@ts-ignore
@@ -89,9 +90,9 @@ export default class AbstractStore extends AbstractMutexer {
89
90
  const cleanedValues = this.willCreate
90
91
  ? yield this.willCreate(values)
91
92
  : values;
92
- const databaseRecord = Object.assign(Object.assign({}, cleanedValues), {
93
+ const databaseRecord = Object.assign(Object.assign({}, cleanedValues), { [this.primaryFieldName]:
93
94
  //@ts-ignore
94
- id: (_a = cleanedValues.id) !== null && _a !== void 0 ? _a : this.db.generateId() });
95
+ (_a = cleanedValues[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() });
95
96
  const toSave = normalizeSchemaValues(this.databaseSchema,
96
97
  //@ts-ignore
97
98
  databaseRecord, { shouldCreateEntityInstances: false });
@@ -111,6 +112,9 @@ export default class AbstractStore extends AbstractMutexer {
111
112
  }
112
113
  });
113
114
  }
115
+ get primaryFieldName() {
116
+ return this.primaryFieldNames[0];
117
+ }
114
118
  findOne(query, options = {}) {
115
119
  return __awaiter(this, void 0, void 0, function* () {
116
120
  const results = yield this.find(query, { limit: 1 }, options);
@@ -15,6 +15,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
15
15
  protected abstract databaseSchema: DatabaseSchema;
16
16
  protected scrambleFields?: string[];
17
17
  protected db: Database;
18
+ protected primaryFieldNames: string[];
18
19
  initialize?(): Promise<void>;
19
20
  protected prepareRecord?<IncludePrivateFields extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(record: DatabaseRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<PrepareResults<FullSchema, IncludePrivateFields>>;
20
21
  protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord, 'id'>>;
@@ -30,6 +31,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
30
31
  protected prepareAndNormalizeRecord<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(record: any, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
31
32
  create<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(values: CreateRecord[], options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>[]>;
32
33
  createOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(values: CreateRecord, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>;
34
+ private get primaryFieldName();
33
35
  findOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> | null>;
34
36
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
35
37
  find<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, queryOptions?: Omit<QueryOptions, 'includeFields'>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Awaited<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>[]>;
@@ -47,6 +47,7 @@ const error_utility_1 = __importDefault(require("../utilities/error.utility"));
47
47
  class AbstractStore extends AbstractMutexer_1.default {
48
48
  constructor(db, collectionName) {
49
49
  super();
50
+ this.primaryFieldNames = ['id'];
50
51
  this.db = db;
51
52
  if (collectionName) {
52
53
  this.setCollectionName(collectionName);
@@ -75,9 +76,9 @@ class AbstractStore extends AbstractMutexer_1.default {
75
76
  const cleanedValues = await Promise.all(values.map(async (v) => (this.willCreate ? this.willCreate(v) : v)));
76
77
  const databaseRecords = cleanedValues.map((v) => {
77
78
  var _a;
78
- return (Object.assign(Object.assign({}, v), {
79
+ return (Object.assign(Object.assign({}, v), { [this.primaryFieldName]:
79
80
  //@ts-ignore
80
- id: (_a = v.id) !== null && _a !== void 0 ? _a : this.db.generateId() }));
81
+ (_a = v[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() }));
81
82
  });
82
83
  const toSave = databaseRecords.map((r) => (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
83
84
  //@ts-ignore
@@ -103,9 +104,9 @@ class AbstractStore extends AbstractMutexer_1.default {
103
104
  const cleanedValues = this.willCreate
104
105
  ? await this.willCreate(values)
105
106
  : values;
106
- const databaseRecord = Object.assign(Object.assign({}, cleanedValues), {
107
+ const databaseRecord = Object.assign(Object.assign({}, cleanedValues), { [this.primaryFieldName]:
107
108
  //@ts-ignore
108
- id: (_a = cleanedValues.id) !== null && _a !== void 0 ? _a : this.db.generateId() });
109
+ (_a = cleanedValues[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() });
109
110
  const toSave = (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
110
111
  //@ts-ignore
111
112
  databaseRecord, { shouldCreateEntityInstances: false });
@@ -124,6 +125,9 @@ class AbstractStore extends AbstractMutexer_1.default {
124
125
  throw coded[0];
125
126
  }
126
127
  }
128
+ get primaryFieldName() {
129
+ return this.primaryFieldNames[0];
130
+ }
127
131
  async findOne(query, options = {}) {
128
132
  const results = await this.find(query, { limit: 1 }, options);
129
133
  if (results.length > 0) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "25.1.0",
6
+ "version": "25.2.0",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",